package com.obuge.yfly.entity;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
@Component
public class CustomerMetaObjectHandler implements MetaObjectHandler {
    private final AtomicLong al = new AtomicLong(1);
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        if(metaObject.getValue("id") == null && "id".equals(metaObject.findProperty("id",true))){
            this.strictInsertFill(metaObject, "id", () -> IdWorker.getId(), Long.class);
        }
//        if(metaObject.getValue("unitId") == null && "unitId".equals(metaObject.findProperty("id",true))){
//            this.strictInsertFill(metaObject, "unitId", () -> "hw", String.class);
//        }
        Date date = new Date();
        this.strictInsertFill(metaObject, "createTime", () -> date.getTime(), Long.class);
//        this.strictInsertFill(metaObject, "createTimeString", () -> format.format(date), String.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        Date date = new Date();
        this.strictUpdateFill(metaObject, "updateTime", () -> System.currentTimeMillis(), Long.class);
//        this.strictInsertFill(metaObject, "updateTimeString", () -> format.format(date), String.class);
    }
}
